/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.cw.view;

import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.lcdui.StringItem;
import org.cw.bean.Establishment;

/**
 *
 * @author SVN - Team
 */
public class FromSearchType extends Form implements CommandListener, ItemStateListener{
    private static final String TITLE = "Search by type of Establishment";
    private static final String typeOfEst[] = {"Café", "Bar", "Restaurant"};
    private MReviewer curDisplay;
    private ChoiceGroup cbbTypeOfEst;
    private ChoiceGroup listEsts;
    private Image iconTitle;
    private StringItem txtTitle;
    private Command cmdView;
    private Command cmdNew;
    private Command cmdBack;
    public FromSearchType(MReviewer curDisplay) {
        super(TITLE);
        this.curDisplay = curDisplay;
        init();
    }

    private void init() {
        try {
            iconTitle = Image.createImage("/find.png");
        } catch (Exception e) {
        }
        append(iconTitle);
        txtTitle = new StringItem("", "Find establishment by type");
        txtTitle.setLayout(StringItem.LAYOUT_2);
        append(txtTitle);
        try {
            Image[] imgs = {Image.createImage("/cafe_coffee.png"), Image.createImage("/drink_bar_cocktails.png"), Image.createImage("/restaurant.png")};
            cbbTypeOfEst = new ChoiceGroup("Select Type:", Choice.POPUP, typeOfEst,imgs);
        }catch(Exception ex){
            cbbTypeOfEst = new ChoiceGroup("Select Type:", Choice.POPUP, typeOfEst,null);
        }
        append(cbbTypeOfEst);
        listEsts = new ChoiceGroup("", Choice.EXCLUSIVE);
        append(listEsts);
        cmdBack = new Command("Back", Command.BACK, 0);
        cmdView = new Command("View Detail", Command.OK, 1);
        cmdNew = new Command("add Review", Command.OK, 2);
        addCommand(cmdBack);
        addCommand(cmdNew);
        addCommand(cmdNew);
        setCommandListener(this);
        setItemStateListener(this);
        initDataSource("Café");
    }

    private void initDataSource(String typeName) {
        listEsts.deleteAll();
        int count = 0;
        for(int i = 0; i < MReviewer.dataStore.size(); i++) {
            Establishment est = (Establishment) MReviewer.dataStore.elementAt(i);
            if(est.getType().equals(typeName)) {
                String strContext = "";
                strContext += "- ID:" + est.getId() + "\n"
                           +"- Name:"+est.getName() +"\n"
                           +"- Type:"+est.getType() +"\n"
                           +"- Type Of Food:"+est.getTypeOfServices() +"\n"
                           +"- Location:"+est.getLocation() +"\n";
                listEsts.append(strContext, null);
                count++;
            }
        }
        listEsts.setLabel("There are " + count + " records in type " + typeName);
    }
    private FromListEstablishments listEst;
    public void commandAction(Command c, Displayable d) {
        if(c == cmdView) {
            
        } else if(c == cmdNew) {
            
        } else if(c == cmdBack) {
            listEst = new FromListEstablishments(curDisplay);
            Display.getDisplay(curDisplay).setCurrent(listEst);
        }
    }

    public void itemStateChanged(Item item) {
        if(item instanceof ChoiceGroup) {
            if(item == cbbTypeOfEst) {
                String strType = cbbTypeOfEst.getString(cbbTypeOfEst.getSelectedIndex());
                initDataSource(strType);
            }
        }
    }

}
